<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<html:header title="单位设置">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">
    <style type="text/css">
        .readonly{
            background-color: #eeeeee;
        }
    </style>
    <script>
        jQuery(function () {
            fixTableOperatorButton();
        });

        layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                util = layui.util;


            renderTable();

            table.on('tool(table)', function (obj) {
                var data = obj.data;
                switch(obj.event){
                    case 'edit':
                        openTab("编辑单位","${baseUrl}goods/unitEdit.action?id="+data.id);
                        break;
                    case 'delete':
                        del(data);
                        break;
                };
            });

            table.on('toolbar(table)', function (obj) {
                let checkStatus = table.checkStatus('table');
                switch(obj.event){
                    case 'add':
                        openTab("新增单位","${baseUrl}goods/unitNew.action");
                        break;
                    case 'deleteBatch':
                        deleteBatch(checkStatus.data);
                        break;
                    case 'enableBatch':
                        enableBatch(checkStatus.data);
                        break;
                    case 'disableBatch':
                        disableBatch(checkStatus.data);
                        break;
                };
            });

            form.on('submit(search)', function (data) {
                var json = data.field;
                table.reload('table',{
                    where: json
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
                return false;
            });
        });

        function renderTable() {
            layui.table.render({
                id:'table',
                elem: '#table',
                toolbar: '#tableToolBar',
                defaultToolbar:[],
                url: "${baseUrl}goods/unit/getUnits.action?time="+(new Date()).valueOf(),
                cols: [[
                    {type:'checkbox', fixed:'left'},
                    {width:70,title:"序号",templet:function (d) {
                        return genTableIndex(d.status==2, "停用", d);
                    }},
                    {field: 'name', width: 150, title: '名称',style:"font-size:12px;"},
                    {field: 'point', width: 100, title: '允许小数',style:"font-size:12px;",templet:function (d) {
                            return d.point ? "是" : "否";
                        }},
                    {field: 'remark',minWidth:120, title: '备注',style:"font-size:12px;"},
                    {title: '操作', minWidth: 80, fixed:'right', templet: '#operBar'},
                ]],
                limit: 10,
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                    //,curr: 5 //设定初始在第 5 页
                    ,groups: 5 //只显示 5 个连续页码
                    ,first: false //不显示首页
                    ,last: false //不显示尾页
                },
                done: function(res, curr, count){
                    setTableLineCss("table",res.data, "status", 2, "font-gray-c2c2c2");
                }
            });
        }

        function del(data) {
            layer.confirm('删除单位《'+data.name+'》?', {icon: 3, title:'删除单位'}, function(index){
                tipLoad();
                post("${baseUrl}goods/unit/delete.action",data, function (resp) {
                    closeAll();
                    if (resp.code == 1){
                        tip("删除成功");
                        layui.table.reload('table',{page: {curr: 1}});
                    } else {
                        errTip(resp.msg);
                    }
                });
            });
        }

        function deleteBatch(data) {
            if (!checkSelectNum(data)){
                return;
            }
            layer.confirm("是否批量删除?", function(index){
                console.log(JSON.stringify(data));
                tipLoad();
                post("${baseUrl}goods/unit/deleteBatch.action",data, function (res) {
                    closeLoading();
                    if (res.code == 1){
                        tip("批量删除成功");
                        layui.table.reload('table',{page: {curr: 1}});
                    } else {
                        errTip(res.msg);
                    }
                });
            });
        }

        function enableBatch(data) {
            if (!checkSelectNum(data)){
                return;
            }
            layer.confirm("是否批量启用?", function(index){
                console.log(JSON.stringify(data));
                tipLoad();
                post("${baseUrl}goods/unit/enableBatch.action",data, function (res) {
                    closeLoading();
                    if (res.code == 1){
                        tip("批量启用成功");
                        layui.table.reload('table',{page: {curr: 1}});
                    }else {
                        errTip(res.msg);
                    }
                });
            });
        }

        function disableBatch(data) {
            if (!checkSelectNum(data)){
                return;
            }
            layer.confirm("是否批量停用?", function(index){
                console.log(JSON.stringify(data));
                tipLoad();
                post("${baseUrl}goods/unit/disableBatch.action",data, function (res) {
                    closeLoading();
                    if (res.code == 1){
                        tip("批量停用成功");
                        layui.table.reload('table',{page: {curr: 1}});
                    }else {
                        errTip(res.msg);
                    }
                });
            });
        }
    </script>

</html:header>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <form class="layui-form layui-form-pane" action="">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">单位名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="searchContent" id="searchContent" autocomplete="off" class="layui-input" style="font-size: 12px;">
                    </div>
                </div>

                <div class="layui-inline">
                    <a class="layui-btn" lay-submit="" lay-filter="search">
                        <i class="layui-icon layui-icon-search"></i>
                    </a>
                </div>
            </div>
        </form>

        <div class="horizon-line"></div>

        <table class="layui-hide" id="table" lay-filter="table"></table>


        <script type="text/html" id="tableToolBar">
            <div class="layui-inline">
                <a class="layui-btn" lay-event="add">新增</a>
            </div>

            <div class="layui-inline">
                <html:drop-menu menu="批量操作" >
                    <a href="javascript:;" lay-event="deleteBatch">批量删除</a>
                    <a href="javascript:;" lay-event="enableBatch">批量启用</a>
                    <a href="javascript:;" lay-event="disableBatch">批量停用</a>
                </html:drop-menu>
            </div>

        </script>

        <script type="text/html" id="operBar">
            <a class="layui-btn layui-btn-xs table-operator" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger table-operator" lay-event="delete">删除</a>
        </script>
    </div>
</div>

</body>
<html:loading tip="处理中,请稍后..."/>
</html>